package ResultModel

import (
	"context"
	"fmt"
	"github.com/zeromicro/go-zero/core/stores/sqlc"
	"github.com/zeromicro/go-zero/core/stores/sqlx"
)

var _ TestResultsModel = (*customTestResultsModel)(nil)

type (
	// TestResultsModel is an interface to be customized, add more methods here,
	// and implement the added methods in customTestResultsModel.
	TestResultsModel interface {
		testResultsModel
		FindSno(ctx context.Context, StudentID string) ([]TestResults, error)
		FindClass(ctx context.Context, ClassID string) ([]TestResults, error)
	}

	customTestResultsModel struct {
		*defaultTestResultsModel
	}
)

// NewTestResultsModel returns a model for the database table.
func NewTestResultsModel(conn sqlx.SqlConn) TestResultsModel {
	return &customTestResultsModel{
		defaultTestResultsModel: newTestResultsModel(conn),
	}
}

func (m *defaultTestResultsModel) FindSno(ctx context.Context, StudentID string) ([]TestResults, error) {
	query := fmt.Sprintf("select %s from %s where student_no = $1 limit 100", testResultsRows, m.table)
	var resp []TestResults
	err := m.conn.QueryRowsCtx(ctx, &resp, query, StudentID)
	switch err {
	case nil:
		return resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

func (m *defaultTestResultsModel) FindClass(ctx context.Context, ClassID string) ([]TestResults, error) {
	query := fmt.Sprintf("select %s from %s where studnet_class = $1 limit 100", testResultsRows, m.table)
	var resp []TestResults
	err := m.conn.QueryRowsCtx(ctx, &resp, query, ClassID)
	switch err {
	case nil:
		return resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}
